package com.yjz.demo.tab

import android.graphics.BitmapFactory
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.yjz.common.tab.FragmentTabView
import com.yjz.common.tab.TabViewAdapter
import com.yjz.log.util.DisplayUtil
import com.yjz.demo.R

import com.yjz.ui.tab.common.TabInfo
import com.yjz.ui.tab.bottom.TabBottomLayout

/**
 *作用 : tabLayout演示
 *作者 : yjz
 *时间 : 2024/9/18 15:59
 */
class BottomTabLayoutActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bottom_tab_layout)
        val tabBottomLayout = findViewById<TabBottomLayout>(R.id.tab_layout)
        tabBottomLayout.setTabAlpha(0.5f)
        val bottomInfoList: MutableList<TabInfo<*>> = ArrayList()
        val homeInfo = TabInfo(
            "首页",
            "fonts/iconfont.ttf",
            getString(R.string.if_home),
            null,
            "#ff656667",
            "#ffd44949"
        )
        homeInfo.fragment = NormalFragment.newInstance("首页")
        val infoRecommend = TabInfo(
            "收藏",
            "fonts/iconfont.ttf",
            getString(R.string.if_favorite),
            null,
            "#ff656667",
            "#ffd44949"
        )
        infoRecommend.fragment = NormalFragment.newInstance("收藏")

        val infoCategory1 = TabInfo(
            "分类",
            "fonts/iconfont.ttf",
            getString(R.string.if_category),
            null,
            "#ff656667",
            "#ffd44949"
        )

        // tab使用图片
        val infoCategory = TabInfo<String>(
            "分类",
            BitmapFactory.decodeResource(resources, R.drawable.fire),
            BitmapFactory.decodeResource(resources, R.drawable.fire),
            "#ff656667",
            "#ffd44949"
        )
        infoCategory.fragment = NormalFragment.newInstance("分类")
        val infoChat = TabInfo(
            "推荐",
            "fonts/iconfont.ttf",
            getString(R.string.if_recommend),
            null,
            "#ff656667",
            "#ffd44949"
        )
        infoChat.fragment = NormalFragment.newInstance("推荐")
        val infoProfile = TabInfo(
            "我的",
            "fonts/iconfont.ttf",
            getString(R.string.if_profile),
            null,
            "#ff656667",
            "#ffd44949"
        )
        infoProfile.fragment = NormalFragment.newInstance("我的")
        bottomInfoList.add(homeInfo)
        bottomInfoList.add(infoRecommend)
        bottomInfoList.add(infoCategory)
        bottomInfoList.add(infoChat)
        bottomInfoList.add(infoProfile)
        tabBottomLayout.inflateInfo(bottomInfoList)
        // 动态改变某个tab的高度，形成凸起的效果
        val tabBottom = tabBottomLayout.findTab(bottomInfoList[2])
        tabBottom?.apply { resetHeight(DisplayUtil.dp2px(66f, resources)) }

        val fragmentTabView = findViewById<FragmentTabView>(R.id.fragment_tab_view)
        val adapter = TabViewAdapter(bottomInfoList, supportFragmentManager);
        fragmentTabView.adapter = adapter

        // 选中监听回调
        tabBottomLayout.addTabSelectChangeListener { index, _, _ ->
            fragmentTabView.setCurrentItem(index);
            // MainActivityLogic.this.currentItemIndex = index;
        }

        tabBottomLayout.defaultSelected(bottomInfoList[0])
    }
}